/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.board;

import java.util.List;
import java.util.Vector;

/**
 * MarkingQueue
 * 
 * @author Daniel Drachau, PRODYNA GmbH
 * @version $Id: $
 */
public class MarkingQueue {

    private List<Marking> markings;

    private int currentMarking, maximumCache;

    private boolean writeMarkings, accessable;

    public MarkingQueue(Integer maximumCache) {
        this.markings = new Vector<Marking>();
        this.currentMarking = 0;
        this.maximumCache = maximumCache;
        this.writeMarkings = false;
        this.accessable = true;
    }

    public MarkingQueue() {
        this(new Integer(7));
    }

    public void undo() {
        if (this.currentMarking == 0)
            return;
        else
            this.currentMarking--;
    }

    public void redo() {
        if (this.currentMarking == this.markings.size())
            return;
        else
            this.currentMarking++;
    }

    /**
     * @return returns the writing which could not be buffered anymore
     */
    public Marking getMarkingToWrite() {
        if (this.markings.size() == 0)
            throw new RuntimeException("no Markings to paing");
        Marking marking = this.markings.remove(0);
        this.currentMarking--;
        return marking;
    }

    /**
     * @return the markings which should be painted
     */
    public List<Marking> getMarkingsToPaint() {
        List<Marking> markings = new Vector<Marking>();
        for (int i = 0; i < this.currentMarking; i++) {
            markings.add(this.markings.get(i));
        }
        return markings;
    }

    /**
     * @return Returns the writeMarkings.
     */
    public boolean isWriteMarkings() {
        return writeMarkings;
    }

    /**
     * @param writeMarkings
     *            The writeMarkings to set.
     */
    public void setWriteMarking(boolean writeMarkings) {
        this.writeMarkings = writeMarkings;
    }

    /**
     * @param marking
     *            the marking to add
     */
    public void addMarking(Marking marking) {
        while (this.currentMarking < this.markings.size())
            this.markings.remove(this.currentMarking);
        this.markings.add(marking);
        // TODO check sound activated Flag
        this.currentMarking++;
        if (this.maximumCache < this.markings.size())
            this.setWriteMarking(true);
    }

    /**
     * @return Returns the accessable.
     */
    public boolean isAccessable() {
        return accessable;
    }

    /**
     * @param accessable
     *            The accessable to set.
     */
    public void setAccessable(boolean accessable) {
        this.accessable = accessable;
    }

    public boolean canUndo() {
        return this.markings.size() > 0 && this.currentMarking > 0;
    }

    public boolean canRedo() {
        return this.markings.size() > 0 && this.currentMarking < this.markings.size();
    }
}
